package com.ityuwen.mengyagushi.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ityuwen.mengyagushi.album.mapper.*;
import com.ityuwen.mengyagushi.album.service.BaseCategoryService;
import com.ityuwen.mengyagushi.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> jsonObjects1 = new ArrayList<>();
		// 一次性查询出所有的一二三级分类。
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(null);
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return jsonObjects1;
		}
		/// 获取所有的一级分类，把一级分类对象转化成JSONObject对象放入jsonObjects1集合
		// 根据一级分类的id进行分组。以分组字段（一级分类的id）作为key
		Map<Long, List<BaseCategoryView>> categoryMap1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		// 把每一个kv转换成JSONObject对象放入jsonObjects1集合
		categoryMap1.forEach((category1Id, baseCategoryViews2) -> {
			JSONObject jsonObject1 = new JSONObject();
			jsonObjects1.add(jsonObject1);
			jsonObject1.put("categoryId", category1Id);
			// 获取一级分类的名称，根据第一条数据获取即可，有该组数据，那么该组数据至少有一条
			jsonObject1.put("categoryName", baseCategoryViews2.get(0).getCategory1Name());
			// 初始化二级分类集合
			List<JSONObject> jsonObjects2 = new ArrayList<>();
			jsonObject1.put("categoryChild", jsonObjects2);

			// 获取一级分类下二级分类数据：二级分类id作为key，
			Map<Long, List<BaseCategoryView>> categoryMap2 = baseCategoryViews2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			categoryMap2.forEach((category2Id, baseCategoryViews3) -> {
				JSONObject jsonObject2 = new JSONObject();
				jsonObjects2.add(jsonObject2);
				jsonObject2.put("categoryId", category2Id);
				jsonObject2.put("categoryName", baseCategoryViews3.get(0).getCategory2Name());
				// 二级分类下的三季分类
				jsonObject2.put("categoryChild", baseCategoryViews3.stream().map(baseCategoryView -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					return jsonObject3;
				}).collect(Collectors.toList()));
			});
		});

		return jsonObjects1;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return this.baseAttributeMapper.findAttribute(category1Id);
	}

	@Override
	public BaseCategoryView findBaseCategoryView(Long category3Id) {
		return this.baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		// 根据一级分类id查询二级分类
		List<BaseCategory2> baseCategory2s = this.baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategory2s)){
			return null;
		}

		// 根据二级分类id查询置顶的三级分类
		List<Long> category2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		return this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2Ids)
				.eq(BaseCategory3::getIsTop, 1).last("limit 7"));
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {

		JSONObject jsonObject = new JSONObject();
		// 查询一级分类下的子分类
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return null;
		}
		// 如果不为空
		jsonObject.put("categoryId", baseCategoryViews.get(0).getCategory1Id());
		jsonObject.put("categoryName", baseCategoryViews.get(0).getCategory1Name());
		// 初始化一个二级分类集合
		List<JSONObject> jsonObjects2 = new ArrayList<>();
		jsonObject.put("categoryChild", jsonObjects2);
		// 获取二级分类数据
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		category2Map.forEach((category2Id, category3s) -> {
			JSONObject jsonObject2 = new JSONObject();
			jsonObjects2.add(jsonObject2);
			jsonObject2.put("categoryId", category2Id);
			jsonObject2.put("categoryName", category3s.get(0).getCategory2Name());
			jsonObject2.put("categoryChild", category3s.stream().map(category3 -> {
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId", category3.getCategory3Id());
				jsonObject3.put("categoryName", category3.getCategory3Name());
				return jsonObject3;
			}).collect(Collectors.toList()));
		});

		return jsonObject;
	}
}
